package com.example.administrator.netchess;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyJson {

    public Integer GetCmd (String data){
        int cmd = 0;

        if (null!=data){
            try {
                JSONObject jo = new JSONObject(data);
                cmd = jo.getInt(Common.FinalString.CMD);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return cmd;
    }


    public void GetBuildRoom (String data,Handler handler) {
        try {
            if (null!=data){
                JSONObject jo = new JSONObject(data);
                int status = jo.getInt(Common.FinalString.KEY_STATUS);
                int room_id = jo.getJSONObject(Common.FinalString.KEY_ROOM_INFO).getInt(Common.FinalString.KEY_ROOM_ID);
                String room_name = jo.getJSONObject(Common.FinalString.KEY_ROOM_INFO).getString(Common.FinalString.KEY_ROOM_NAME);

                Message message = new Message();
                message.what = Common.FinalString.MESSAGE_MAIN;
                message.arg1 = status;
                message.arg2 = room_id;
                message.obj = room_name;
                handler.sendMessage(message);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void GetRoomList (String data,Handler handler) throws JSONException {

        if (null!=data){
            JSONObject jo = new JSONObject(data);
            JSONArray ja = jo.getJSONArray(Common.FinalString.KEY_ROOM_LIST);

            Message message = new Message();
            message.what = Common.FinalString.MESSAGE_MAIN_LIST;
            message.obj = ja;
            handler.sendMessage(message);

        }
    }





}
